<?php

namespace app\common\model;

use Think\Db;
use think\Model;

/**
 * 错误类别业务处理
 */
class Tasks extends Base
{
    //    protected $table = 'my_machines_type';

    /**
     * 获取错误类别
     */
    public function getById($id)
    {
        return (Object)$this->where('id', $id)->find()->toArray();
    }


    /**
     * 删除错误类别
     */
    public function del()
    {
        $id     = input('id');
        $result = $this->where("id='{$id}'")->delete();
        if (false !== $result) {
            return [1, "删除成功:" . input('id')];
        }
        else {
            return [0, $this->getError()];
        }
    }


    /**
     *  获取主错误类别
     */
    public function getMainFaults()
    {
        return (Object)$this->where('pid', 0)->order('sort', 'desc')->select()->toArray();
    }


    public function getAll()
    {
        $data = $this->order(['id' => 'asc'])->select()->toArray();
        return $data;
    }


    public function getDatas($type = '')
    {
        //        $uid   = session('my_self')['id'];
        $page  = input('get.page');
        $limit = input('get.limit');
        $where = $this->getWhere();
        if ($type == 'page') {//分页
            $rs = $this->where($where)->page($page, $limit)->order(['id' => 'asc'])->select()->toArray();
            return $rs;
        }
        else {//总数
            return $this->where($where)->select()->toArray();
        }
    }


    public function getWhere()
    {

        $where = ' 1 ';

        if (input('get.key/a')) {
            foreach (input('get.key/a') as $k => $v) {
                if (!empty($v)) {
                    switch ($k) {
                        case "name":
                            $where .= " and {$k} like '%{$v}%'";
                            break;
                        case "type":
                            $where .= " and {$k} like '%{$v}%'";
                            break;
                        case "machine_type":
                            $where .= " and {$k}='{$v}'";
                            break;
                        case "time":
                            $applydate = explode("--", $v);
                            $start     = strtotime($applydate[0]);
                            $end       = strtotime($applydate[1]);
                            $where     .= " and '{$start}' <=  unix_timestamp(`time`) and '$end' >= unix_timestamp(`time`) ";
                            break;
                    }
                }
            }
        }

        return $where;
    }

    public function getFaultsByType($typeid = '')
    {
        if (empty($typeid))
            return $this->getAllFaults();
        return $this->where(['type' => $typeid])->order(['sort' => 'desc', 'id' => 'asc'])->select()->toArray();
    }


    public function getParent()
    {

    }


    public function machinetype()
    {
        return $this->hasOne('MachinesType','id','machine_type');
    }



    public function getNames($key = null) {
        static $names = [];
        if (empty($names)) {
            $rs = $this->getAllFaults();
            foreach ($rs as $v) {
                $names[$v['id']] = $v['name'];
            }
        }
        return empty($key) ? $names : (isset($names[$key]) ? $names[$key] : '');
    }


    public function getNameList($ids)
    {

        if (empty($ids))
            return '';
        $names = $this->getNames();
        $tmp   = [];
        foreach (explode(',', $ids) as $v) {
            if (isset($names[$v])) {
                $tmp[] = $names[$v];
            }
        }

        return implode(',', $tmp);
    }

}
